<?php
App::uses('Controller', 'Controller');

class AppController extends Controller
{
    public $uses = array('tbl_admin_member');

    public function beforeFilter()
    {
        parent::beforeFilter();

        if(!empty($this->passedArgs['search_value']))
        {
            CakeSession::write('search_value', $this->passedArgs['search_value']);
        }

        if(!empty($this->data['tbl_auth']))
        {
            $admin_id = $this->data['tbl_auth']['admin_id'];
            $admin_pass = $this->data['tbl_auth']['admin_pass'];

            if(!$admin_info = $this->tbl_admin_member->admin_login($admin_id, $admin_pass))
            {
                $login_err_msg = __(LANG_LOGIN_FAIL);
                $this->Session->delete('tbl_auth');
            }
            else
            {
                $login_msg = __(LANG_LOGIN_SUCCESS);
                $this->Session->write('tbl_auth.admin_id', $admin_info['tbl_admin_member']['admin_id']);
                $this->Session->write('tbl_auth.admin_pass', $admin_info['tbl_admin_member']['admin_pass']);
                $this->Session->write('tbl_auth.admin_name', $admin_info['tbl_admin_member']['admin_name']);
            }
        }

        if(!empty($this->passedArgs['out']))
        {
            $this->Session->delete('tbl_auth');

            if(!empty($this->passedArgs['id']))
            {
                $this->redirect(array(
                    'controller' => 'User',
                    'action' => 'user_select',
                    'id' => $this->passedArgs['id'],
                    'search_value' => $this->passedArgs['search_value'],
                ));
            }

            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));

        }

        $this->set(compact('login_err_msg', 'login_msg'));
    }
}
